package server

import (
	"context"
	"gitee.com/cristiane/micro-mall-search/pkg/code"
	"gitee.com/cristiane/micro-mall-search/proto/micro_mall_search_proto/search_business"
	"gitee.com/cristiane/micro-mall-search/service"
)

type SearchBusinessServer struct {
}

func NewSearchBusinessServer() *SearchBusinessServer {
	return &SearchBusinessServer{}
}

func (s *SearchBusinessServer) SkuInventorySearch(ctx context.Context, req *search_business.SkuInventorySearchRequest) (*search_business.SkuInventorySearchResponse, error) {
	result := &search_business.SkuInventorySearchResponse{
		Common: &search_business.CommonResponse{
			Code: search_business.RetCode_SUCCESS,
		},
	}
	list, retCode := service.SkuInventorySearch(ctx, req.GetSkuKey())
	if retCode != code.Success {
		result.Common.Code = search_business.RetCode_ERROR
		return result, nil
	}
	result.List = list
	return result, nil
}

func (s *SearchBusinessServer) ShopSearch(ctx context.Context, req *search_business.ShopSearchRequest) (*search_business.ShopSearchResponse, error) {
	result := &search_business.ShopSearchResponse{
		Common: &search_business.CommonResponse{
			Code: search_business.RetCode_SUCCESS,
		},
	}
	list, retCode := service.ShopSearch(ctx, req.GetShopKey())
	if retCode != code.Success {
		result.Common.Code = search_business.RetCode_ERROR
		return result, nil
	}
	result.List = list
	return result, nil
}

func (s *SearchBusinessServer) UserSearch(ctx context.Context, req *search_business.UserSearchRequest) (*search_business.UserSearchResponse, error) {
	result := &search_business.UserSearchResponse{
		Common: &search_business.CommonResponse{
			Code: search_business.RetCode_SUCCESS,
		},
	}
	list, retCode := service.UserSearch(ctx, req.GetQuery())
	if retCode != code.Success {
		result.Common.Code = search_business.RetCode_ERROR
		return result, nil
	}
	result.List = list
	return result, nil
}

func (s *SearchBusinessServer) MerchantInfoSearch(ctx context.Context, req *search_business.MerchantInfoSearchRequest) (*search_business.MerchantInfoSearchResponse, error) {
	result := &search_business.MerchantInfoSearchResponse{
		Common: &search_business.CommonResponse{
			Code: search_business.RetCode_SUCCESS,
		},
	}
	list, retCode := service.MerchantSearch(ctx, req.GetQuery())
	if retCode != code.Success {
		result.Common.Code = search_business.RetCode_ERROR
		return result, nil
	}
	result.List = list
	return result, nil
}

func (s *SearchBusinessServer) TradeOrderSearch(ctx context.Context, req *search_business.TradeOrderSearchRequest) (*search_business.TradeOrderSearchResponse, error) {
	result := &search_business.TradeOrderSearchResponse{Common: &search_business.CommonResponse{
		Code: search_business.RetCode_SUCCESS,
	}}
	list, retCode := service.TradeOrderSearch(ctx, req.GetQuery())
	if retCode != code.Success {
		result.Common.Code = search_business.RetCode_ERROR
		return result, nil
	}
	result.List = list
	return result, nil
}
